package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.DeliveryWarehouse;
import com.dingreading.cloud.admin.entity.table.DeliveryWarehouseTableDef;
import com.dingreading.cloud.admin.mapper.DeliveryWarehouseMapper;
import com.dingreading.cloud.admin.service.DeliveryWarehouseService;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-02-21
 */
@Service
public class DeliveryWarehouseServiceImpl extends BaseServiceImpl<DeliveryWarehouseMapper, DeliveryWarehouse, DeliveryWarehouseTableDef> implements DeliveryWarehouseService {

    @Override
    protected DeliveryWarehouseTableDef getTable() {
        return DeliveryWarehouseTableDef.deliveryWarehouse;
    }

    @Override
    public Page<DeliveryWarehouse> pageList(PageUtil pageUtil, String name, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(name))
            condition.and(table.name.like(name)
                    .or(table.userName.like(name))
                    .or(table.phone.like(name))
            );
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public boolean updateStatus(Long id, Integer enabled) {
        return UpdateChain.of(DeliveryWarehouse.class)
                .set(table.enabled, enabled)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public List<DeliveryWarehouse> getByType(int type) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.enabled.eq(1))
                .and(table.type.eq(type));
        return getMapper().selectListByCondition(condition);
    }
}
